package com.oasistudio.ads.expert;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Statistics extends Activity {
	AdsExpertApplication adsApp;

	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		System.out.println("Statistics.onCreate() start");
		setContentView(R.layout.statistics);
		adsApp = (AdsExpertApplication) getApplicationContext();
		initTable();
		System.out.println("Statistics.onCreate() end");

	}

	private void initTable() {
		addRow(makeRow(0, "联盟", "广告请求次数", "请求成功率"));//,  "点击数", "响应avg"));
		int rowIndex = 1;
		JSONArray result = this.request();
		try {
			for (int i = 0; i < result.length(); i++) {
				JSONArray jsonArray = result.getJSONArray(i);
				TableRow tr = makeRow(rowIndex++, 
						jsonArray.getString(0), jsonArray.getString(1),
						jsonArray.getString(2));//, jsonArray.getString(3), 
						//jsonArray.getString(4));
				// Add the TableRow to the TableLayout
				addRow(tr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JSONArray request() {
		String href = this.getResources().getString(R.string.href_stat);
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(href);
		System.out.println("ServerAccessor.access() href = " + href);
		try {
			// Add your data
			// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			// Execute HTTP Post Request
			HttpResponse response = httpclient.execute(httppost);
			String retSrc = EntityUtils.toString(response.getEntity());
			System.out.println("&&&&&&&&&&&&SreverAccessor.access() retSrc = " + retSrc);
			// 生成 JSON 对象
			JSONArray jsonArray = new JSONArray(retSrc);
			
			System.out.println("response.getAllHeaders().length = " + response.getAllHeaders().length);
			return jsonArray;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		return new JSONArray();
	}

	private void addRow(TableRow row) {
		TableLayout tableLayout = (TableLayout) this.findViewById(R.id.table_layout);
		tableLayout.addView(row, new TableLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));
	}

	private TableRow makeRow(int rowIndex, String col1, String col2, String col3){//, String col4, String col5) {
		TableRow tr = new TableRow(this);
		tr.setId(rowIndex);
		tr.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));
		// tr.setBackgroundDrawable(this.getResources().getDrawable(R.layout.table_border));

		// Create a TextView to house the name of the province
		TextView column01 = new TextView(this);
		column01.setId(100 + rowIndex);
		column01.setText(col1);
		column01.setTextColor(Color.WHITE);
		column01.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));
		column01.setBackgroundDrawable(this.getResources().getDrawable(R.layout.table_cell_border));
		tr.addView(column01, new TableRow.LayoutParams(0));

		// Create a TextView to house the value of the after-tax income
		TextView column02 = new TextView(this);
		column02.setId(200 + rowIndex);
		column02.setText(col2);
		column02.setTextColor(Color.WHITE);
		column02.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));
		column02.setBackgroundDrawable(this.getResources().getDrawable(R.layout.table_cell_border));
		tr.addView(column02, new TableRow.LayoutParams(1));

		// Create a TextView to house the value of the after-tax income
		TextView column03 = new TextView(this);
		column03.setId(300 + rowIndex);
		column03.setText(col3);
		column03.setTextColor(Color.WHITE);
		column03.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));
		column03.setBackgroundDrawable(this.getResources().getDrawable(R.layout.table_cell_border));
		tr.addView(column03, new TableRow.LayoutParams(2));

		
//		// Create a TextView to house the value of the after-tax income
//		TextView column04 = new TextView(this);
//		column04.setId(400 + rowIndex);
//		column04.setText(col4);
//		column04.setTextColor(Color.WHITE);
//		column04.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
//				ViewGroup.LayoutParams.WRAP_CONTENT));
//		column04.setBackgroundDrawable(this.getResources().getDrawable(R.layout.table_cell_border));
//		tr.addView(column04, new TableRow.LayoutParams(3));
//
//		// Create a TextView to house the value of the after-tax income
//		TextView column05 = new TextView(this);
//		column05.setId(500 + rowIndex);
//		column05.setText(col5);
//		column05.setTextColor(Color.WHITE);
//		column05.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
//				ViewGroup.LayoutParams.WRAP_CONTENT));
//		column05.setBackgroundDrawable(this.getResources().getDrawable(R.layout.table_cell_border));
//		tr.addView(column05, new TableRow.LayoutParams(4));
		return tr;
	}
	
}